package cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.h5;

import cn.com.dcsgo.sdk.payment.wechat.ltzf.factory.Configuration;
import cn.com.dcsgo.sdk.payment.wechat.ltzf.payments.h5.model.*;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;

/**
 * H5支付 service
 *
 * @author Dcsgo
 * @since 2024/8/15 下午3:20
 */

public class H5PayService {
    /**
     * 扫码支付API
     */
    private final IH5PayApi h5payApi;

    /**
     * 配置类(蓝兔支付、HTTP相关配置)
     */
    private final Configuration configuration;

    /**
     * 预付
     *
     * @param request 预付请求实体类
     * @return 预付响应实体类(PrepayResponse)
     * @throws IOException IO异常
     */
    public PrepayResponse prepay(PrepayRequest request) throws IOException {
        Call<PrepayResponse> call = h5payApi.prepay(
                request.getMchId(),
                request.getOutTradeNo(),
                request.getTotalFee(),
                request.getBody(),
                request.getTimestamp(),
                request.getNotifyUrl(),
                request.createSign(configuration.getPartnerKey())
        );
        Response<PrepayResponse> execute = call.execute();
        return execute.body();
    }

    /**
     * 订单退款
     *
     * @param request 订单退款请求实体类
     * @return 订单退款响应实体类(RefundOrderResponse)
     * @throws IOException IO异常
     */
    public RefundOrderResponse refundOrder(RefundOrderRequest request) throws IOException {
        Call<RefundOrderResponse> call = h5payApi.refundOrder(
                request.getMchId(),
                request.getOutTradeNo(),
                request.getOutRefundNo(),
                request.getTimestamp(),
                request.getRefundFee(),
                request.getRefundDesc(),
                request.getNotifyUrl(),
                request.createSign(configuration.getPartnerKey())
        );
        Response<RefundOrderResponse> execute = call.execute();
        return execute.body();
    }

    /**
     * 查询订单
     *
     * @param request 查询订单请求实体(QueryPayOrderRequest)
     * @return 查询订单响应实体类(QueryPayOrderResponse)
     * @throws IOException IO异常
     */
    public QueryPayOrderResponse queryPayOrder(QueryPayOrderRequest request) throws IOException {
        Call<QueryPayOrderResponse> call = h5payApi.queryPayOrder(
                request.getMchId(),
                request.getOutTradeNo(),
                request.getTimestamp(),
                request.createSign(configuration.getPartnerKey())
        );
        Response<QueryPayOrderResponse> execute = call.execute();
        return execute.body();
    }

    /**
     * 查询退款结果
     *
     * @param request 查询退款结果请求实体类(QueryRefundOrderRequest)
     * @return 查询退款结果响应实体类(QueryRefundOrderResponse)
     * @throws IOException IO异常
     */
    public QueryRefundOrderResponse queryRefundOrder(QueryRefundOrderRequest request) throws IOException {
        Call<QueryRefundOrderResponse> call = h5payApi.queryRefundOrder(
                request.getMchId(),
                request.getOutRefundNo(),
                request.getTimestamp(),
                request.createSign(configuration.getPartnerKey())
        );
        Response<QueryRefundOrderResponse> execute = call.execute();
        return execute.body();
    }

    /**
     * 构造一个H5支付的 service
     *
     * @param h5payApi      H5支付API
     * @param configuration 配置类(蓝兔支付、HTTP相关配置)
     */
    public H5PayService(IH5PayApi h5payApi, Configuration configuration) {
        this.h5payApi = h5payApi;
        this.configuration = configuration;
    }
}
